package com.edou.cms.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.cms.dao.ICmsArticleDao;
import com.edou.cms.dao.ICmsColumnDao;
import com.edou.cms.dao.ICmsColumnTagDao;
import com.edou.cms.dao.ICmsTagDao;
import com.edou.cms.entity.CmsArticle;
import com.edou.cms.entity.CmsColumn;
import com.edou.cms.entity.CmsColumnTag;
import com.edou.cms.entity.CmsTag;
import com.edou.cms.service.ICmsColumnService;

import com.edou.cms.dto.CmsColumnCondition;

@Service("cmsColumnServiceImpl")
@Transactional(value = "transactionManager")
public class CmsColumnServiceImpl implements ICmsColumnService{

	@Resource(name = "cmsColumnDaoImpl")
	private ICmsColumnDao cmsColumnDao;
	@Resource(name = "cmsColumnTagDaoImpl")
	private ICmsColumnTagDao cmsColumnTagDao;
	@Resource(name = "cmsTagDaoImpl")
	private ICmsTagDao cmsTagDao;
	@Resource(name = "cmsArticleDaoImpl")
	private ICmsArticleDao cmsArticleDao;

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listCmsColumnByPage(PageResult<CmsColumn> pageResult,CmsColumnCondition condition){
		int rows = cmsColumnDao.listCmsColumnByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<CmsColumn> list = cmsColumnDao.listCmsColumnByPage(rowBounds,condition);
		Date now = new Date();
		//查询最新添加的文章
		for (CmsColumn cmsColumn : list) {
			List<CmsArticle> articleList = cmsArticleDao.listNewArticleByColumnIdLimit(cmsColumn.getId(),"", 4);
			for (CmsArticle cmsArticle : articleList) {
				//判断是否显示最新标签
				if(cmsArticle.getCreateDate() != null){
					int formatDate = Integer.parseInt(DateUtils.formatDate(cmsArticle.getCreateDate(),"yyyyMMdd"));
					int nowDate = Integer.parseInt(DateUtils.formatDate(now,"yyyyMMdd"));
					if(nowDate-formatDate <= 3){
						cmsArticle.setShowNew(1);
					}else{
						cmsArticle.setShowNew(0);
					}
				}else{
					cmsArticle.setShowNew(0);
				}
			}
			cmsColumn.setArticleList(articleList);
			List<CmsTag> cmsTag = cmsTagDao.listCmsTagByColumnId(cmsColumn.getId());
			cmsColumn.setColumnTag(cmsTag);
			/*//判断是否显示最新标签
			int formatDate = Integer.parseInt(DateUtils.formatDate(cmsColumn.getCreateDate(),"yyyyMMdd"));
			int nowDate = Integer.parseInt(DateUtils.formatDate(now,"yyyyMMdd"));
			if(nowDate-formatDate <= 3){
				cmsColumn.setIsNew(1);
			}else{
				cmsColumn.setIsNew(0);
			}*/
		}
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 专栏管理
	 */
	public CmsColumn getCmsColumnById(String id){
		// 游览数加1(热度)
		cmsColumnDao.updataCmsViewById(id);
		return cmsColumnDao.getCmsColumnById(id);
	}

	/**
	 * 新增
	 * @param item 专栏管理
	 */
	public void add(CmsColumn item){
		cmsColumnDao.add(item);
		String tagList = item.getTagList();
		if(StringUtils.isNotBlank(tagList)){
			String[] tagIds = tagList.split(",");
			for (int i = 0; i < tagIds.length; i++) {
				CmsColumnTag tag = new CmsColumnTag();
				tag.setColumnId(item.getId());
				tag.setTagId(tagIds[i]);
				tag.setDeleteFlag(0);
				cmsColumnTagDao.add(tag);
				//修改标签下的专栏数量
				cmsTagDao.updateCmsTagColumn(tagIds[i],"zj");
			}
		}
	}

	/**
	 * 修改
	 * @param item 专栏管理
	 */
	public void update(CmsColumn item){
		cmsColumnDao.update(item);
		//关联标签数量重置、
		List<CmsColumnTag> columnTag = cmsColumnTagDao.listColumnTagByColumnId(item.getId());
		for (CmsColumnTag cmsColumnTag : columnTag) {
			cmsTagDao.updateCmsTagColumn(cmsColumnTag.getId(),"jx");
		}
		//删除已有标签
		cmsColumnTagDao.delByColumnId(item.getId());
		//添加新标签
		String tagList = item.getTagList();
		if(StringUtils.isNotBlank(tagList)){
			String[] tagIds = tagList.split(",");
			for (int i = 0; i < tagIds.length; i++) {
				CmsColumnTag tag = new CmsColumnTag();
				tag.setColumnId(item.getId());
				tag.setTagId(tagIds[i]);
				tag.setDeleteFlag(0);
				cmsColumnTagDao.add(tag);
				cmsTagDao.updateCmsTagColumn(tagIds[i],"zj");
			}
		}
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		cmsColumnDao.delByIds(ids);
		for (String id : ids) {
			//关联标签数量重置、
			List<CmsColumnTag> columnTag = cmsColumnTagDao.listColumnTagByColumnId(id);
			for (CmsColumnTag cmsColumnTag : columnTag) {
				cmsTagDao.updateCmsTagColumn(cmsColumnTag.getId(),"jx");
			}
		}
	}

	public void likeOne(String columnId) {
		cmsColumnDao.likeOne(columnId);
	}

}

